package com.googlecode.moneyManager.spreadSheetReader.service;

import com.google.gdata.client.spreadsheet.CellQuery;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Author: Sergiy Doroshenko
 * Date: Jul 20, 2010
 * Time: 3:06:58 PM
 */
@Deprecated
public class GoogleReader {
    public static void main(String[] args) {

        SpreadsheetService service = new SpreadsheetService("garbage");
        try {
            service.setUserCredentials("webserg@gmail.com", "v'hb1980");
        } catch (AuthenticationException e) {
            System.out.println(e.getAuthHeader());
        }
        URL metafeedUrl = null;
        try {
            metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        SpreadsheetFeed feed = null;
        try {
            feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);

            List<SpreadsheetEntry> spreadsheets = feed.getEntries();
            WorksheetEntry worksheet = null;
            for (int i = 0; i < spreadsheets.size(); i++) {
                SpreadsheetEntry entry = spreadsheets.get(i);
                System.out.println("\t" + entry.getTitle().getPlainText());
                if (entry.getTitle().getPlainText().equals("garbage")) {
                    List<WorksheetEntry> worksheets = entry.getWorksheets();
                    // for (int j = 0; j < worksheets.size(); j++) {
                    worksheet = worksheets.get(0);
                    String title = worksheet.getTitle().getPlainText();
                    int rowCount = worksheet.getRowCount();
                    int colCount = worksheet.getColCount();
                    System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);

                    //}
                    break;
                }
            }
            URL cellFeedUrl = worksheet.getCellFeedUrl();
            // CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);
            CellQuery query = new CellQuery(cellFeedUrl);
            query.setMinimumRow(1);
            query.setMaximumRow(6);
            query.setMinimumCol(8);
            query.setMaximumCol(10);
            CellFeed cellFeed = service.query(query, CellFeed.class);

            Number baksPart = null, evroPart = null, grivnaPart = null, baksRate = null, evroRate = null, grivnaAll = null, baksAll = null;

            for (CellEntry cell : cellFeed.getEntries()) {
                // System.out.println(entry.getTitle().getPlainText());
                String shortId = cell.getId().substring(cell.getId().lastIndexOf('/') + 1);
                //String id = cell.getId();
                System.out.println(" -- Cell(" + shortId + "/" + cell.getTitle().getPlainText()
                        + ") formula(" + cell.getCell().getInputValue() + ") numeric("
                        + cell.getCell().getNumericValue() + ") value("
                        + cell.getCell().getValue() + ")");
                if (cell.getTitle().getPlainText().equals("H1")) {
                    baksPart = cell.getCell().getNumericValue();
                } else if (cell.getTitle().getPlainText().equals("H2")) {
                    evroPart = cell.getCell().getNumericValue();
                } else if (cell.getTitle().getPlainText().equals("H3")) {
                    grivnaPart = cell.getCell().getNumericValue();
                } else if (cell.getTitle().getPlainText().equals("H5")) {
                    grivnaAll = cell.getCell().getNumericValue();
                } else if (cell.getTitle().getPlainText().equals("H6")) {
                    baksAll = cell.getCell().getNumericValue();
                } else if (cell.getTitle().getPlainText().equals("J1")) {
                    baksRate = cell.getCell().getNumericValue();
                } else if (cell.getTitle().getPlainText().equals("J2")) {
                    evroRate = cell.getCell().getNumericValue();
                }
            }


            System.out.println(baksAll);
            System.out.println(grivnaAll);
            System.out.println(baksPart);
            System.out.println(grivnaPart);
            System.out.println("evroPart=" + evroPart);
            System.out.println("baksRate = " + baksRate);
            System.out.println(evroRate);


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServiceException se) {
            se.printStackTrace();
        }


    }
}
